package com.push.spider.project;

import com.alibaba.fastjson.JSONObject;
import com.push.common.utils.DateUtils;
import com.push.common.utils.ImageUtil;
import com.push.common.utils.httpclient.HttpClientResult;
import com.push.common.utils.httpclient.HttpClientUtil;

import java.io.IOException;

public class CkcestSpider {
    /*设置网页抓取响应时间*/
    private static final int TIMEOUT = 10000;

    public CkcestSpider(Long id) {
        //this.bizTopExpertsInformation = bizTopExpertsInformationService.selectBizTopExpertsInformationById(id);
    }

    public static int getTimeInSeconds(){
        return new Long(System.currentTimeMillis() / 1000).intValue();
    }

    public void run() {
        try {
            HttpClientResult response = HttpClientUtil.doGet("https://sso.ckcest.cn/api/auth_web/captcha?width=120&height=51&type=char&app_key=8Q0RJ3VSAIcjxH2c&timestamp="+getTimeInSeconds(), true);
            System.out.println(response.getContent());
            JSONObject dataObj = JSONObject.parseObject(response.getContent());
            String base64Image = dataObj.getJSONObject("data").getString("base64Image").replaceAll("data:image/jpeg;base64,", "");
            //ImageUtil.decodeBase64ToImage(dataObj.getString("base64Image"), RuoYiConfig.getUploadPath(), "/"+DateUtils.getTimeInMillis()+".jpg");
            String imagename = DateUtils.getTimeInMillis()+".jpeg";
            ImageUtil.decodeBase64ToImage(base64Image, "D:\\temp\\",imagename);
            System.out.println("verification="+ImageUtil.readVerifyImage("D:\\temp\\"+imagename));
            /*

            Document document = Jsoup.connect("http://gsp.ckcest.cn/search.html?shortname=FUNORG&word=%E5%9B%BD%E5%AE%B6%E8%87%AA%E7%84%B6%E7%A7%91%E5%AD%A6%E5%9F%BA%E9%87%91").timeout(TIMEOUT).get();

            Elements e = document.getElementsByTag("script").eq(25);
            String requestId = "";
            int idx = 0;
            for (Element element : e) {
                String[] data = element.data().toString().split("\\r?\\n");
                for (String line : data) {
                    line = line.trim();
                    if(line.startsWith("var requestId")){
                        requestId = line.substring(17, line.length()-1);
                        System.out.println(requestId);
                    }
                }
                idx++;
            }

            if (StringUtils.isNotEmpty(requestId)){
                CkcestParam ckcestParam = new CkcestParam(1,20);
                int pages = 0, current = 0;
                String response = HttpClientUtil.sendPostByJson("http://gsp.ckcest.cn/data/es/search?requestId="+requestId+"&operate=false", JSON.toJSONString(ckcestParam), 2);
                System.out.println(response);
                if (StringUtils.isNotEmpty(response)){
                    JSONObject dataObj = JSONObject.parseObject(response);
                    pages = dataObj.getJSONObject("datas").getInteger("pages");
                    current = dataObj.getJSONObject("datas").getInteger("current");
                    do{
                        response = HttpClientUtil.sendPostByJson("http://gsp.ckcest.cn/data/es/search?requestId="+requestId+"&operate=false", JSON.toJSONString(new CkcestParam(current+1,20)), 2);
                        System.out.println(response);
                        dataObj = JSONObject.parseObject(response);
                        current = dataObj.getJSONObject("datas").getInteger("current");
                    }while (current <= pages);
                }
                System.out.println(response);

            }*/
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args){
        CkcestSpider spider = new CkcestSpider(1L);
        spider.run();
    }
}
